package me.uniauto.ctrl;

import io.swagger.annotations.*;
import me.uniauto.Constants.HttpConstants;
import me.uniauto.bean.MUser;
import me.uniauto.service.IUserService;
import me.uniauto.util.CommResp;
import me.uniauto.util.MyStringUtil;
import me.uniauto.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by hardy on 2018/2/22.
 */
@Controller
@RequestMapping(value = "user")
@Api(value = "用户", tags = "用户", description = "用户")
public class UserController {
    @Autowired
    private IUserService userService;


    @ResponseBody
    @RequestMapping(value = "getUserOneInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "获取用户信息", httpMethod = "POST", notes = "获取单个用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "1", required = true, dataType = "string", paramType = "query"),
    })
    public Object getUserOneInfo(HttpServletRequest request) throws Exception {
        Map map = selectParamMap(request);
        if(MyStringUtil.isEmpty(map.get("mobile")+"")||MyStringUtil.isEmpty(map.get("password")+"")){
            return CommResp.args_error_resp;
        }
        MUser userVO = (MUser) request.getAttribute(HttpConstants._user_vo);
        return userService.getUserOneInfo(userVO);
    }

    @ResponseBody
    @RequestMapping(value = "getUserInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "分页获取用户信息方法", httpMethod = "POST", notes = "分页获取用户信息")
    public Object getUserInfo() throws Exception {
        return userService.getUserInfo();
    }

//    @ResponseBody
//    @RequestMapping(value = "updateUserCodeInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
//    @ApiOperation(value = "更改用户验证码信息", httpMethod = "POST", notes = "更改用户验证码信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userCodeId", value = "手机验证码id", required = true, dataType = "String", paramType = "form"),
//            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "form"),
//            @ApiImplicitParam(name = "userCode", value = "验证码", required = true, dataType = "String", paramType = "form"),
//    })
//    public Object updateUserCodeInfo(HttpServletRequest request) throws Exception {
//        Map map = selectParamMap(request);
//        if(MyStringUtil.isEmpty(map.get("mobile")+"")||MyStringUtil.isEmpty(map.get("password")+"")){
//            return CommResp.args_error_resp;
//        }
//        return userService.updateUserCodeInfo(request);
//    }
//
//    @ResponseBody
//    @RequestMapping(value = "getUserCodeInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
//    @ApiOperation(value = "从缓存中获取用户验证码信息", httpMethod = "POST", notes = "从缓存中获取用户验证码信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userCodeId", value = "手机验证码id", required = true, dataType = "String", paramType = "form"),
//    })
//    public Object getUserCodeInfo(HttpServletRequest request) throws Exception {
//        return userService.getUserCodeInfo(request);
//    }
//    @ResponseBody
//    @RequestMapping(value = "login", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
//    @ApiOperation(value = "登陆", httpMethod = "POST", notes = "登陆")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "mobile", value = "用户手机号", required = true, dataType = "String", paramType = "form"),
//            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "form"),
//
//    })
//    public Object login(HttpServletRequest request) throws Exception {
//        return userService.login(request);
//    }



    /**
     * 获取map
     */
    public Map selectParamMap(HttpServletRequest request){
        if(MyStringUtil.isBlank(request.getParameter(HttpConstants._swagger_ui))){
            System.out.println("**********************自己去解析*******************************");
            return RequestUtil.paramsMap(request);
        }else{
            System.out.println("**********************从request中获取*******Map******************************");
            return (Map) request.getAttribute("parmasMap");
        }
    }

}
